<?php

namespace Drupal\shusy_team\Entity;

use Entity;

class ShusyTeam extends Entity {

  public $id;

  public $vid;

  public $uid;

  public $op_uid;

  public $created;

  public $changed;

  public $is_new_revision;

  public $pid = 0;

  public $pid_market = 0;

  public $pid_company = 0;

  public $level = 1;

  public $market_1 = 0;

  public $market_2 = 0;

  public $company_1 = 0;

  public $company_2 = 0;

  public $company_3 = 0;

  public $status = 0;

  function __construct($values = []) {
    $this->is_new_revision = TRUE;
    parent::__construct($values, 'shusy_team');
  }

  public function save() {
    $this->changed = REQUEST_TIME;
    if (empty($this->created)) {
      $this->created = REQUEST_TIME;
    }
    $this->op_uid = $GLOBALS['user']->uid;
    return parent::save();
  }

  static function getParent(int $uid) {
    $entities = entity_load('shusy_team', FALSE, ['uid' => $uid]);
    if (!empty($entities)) {
      $entity = reset($entities);
      if ($entity instanceof ShusyTeam) {
        return $entity->pid;
      }
    }
    return -1;
  }

  static function getTeam(int $uid) {
    $entities = entity_load('shusy_team', FALSE, ['uid' => $uid]);
    if (!empty($entities)) {
      $entity = reset($entities);
      if ($entity instanceof ShusyTeam) {
        return $entity;
      }
    }
    return FALSE;
  }
}
